package cn.iocoder.yudao.module.crossborder.order.controller.admin.ordershipmentitem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crossborder.order.controller.admin.ordershipmentitem.vo.*;
import cn.iocoder.yudao.module.crossborder.order.dal.dataobject.ordershipmentitem.OrderShipmentItemDO;
import cn.iocoder.yudao.module.crossborder.order.service.ordershipmentitem.OrderShipmentItemService;

@Tag(name = "管理后台 - 跨境电商发货明细")
@RestController
@RequestMapping("/crossborder/order-shipment-item")
@Validated
public class OrderShipmentItemController {

    @Resource
    private OrderShipmentItemService orderShipmentItemService;

    @PostMapping("/create")
    @Operation(summary = "创建跨境电商发货明细")
    @PreAuthorize("@ss.hasPermission('crossborder:order-shipment-item:create')")
    public CommonResult<Long> createOrderShipmentItem(@Valid @RequestBody OrderShipmentItemSaveReqVO createReqVO) {
        return success(orderShipmentItemService.createOrderShipmentItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跨境电商发货明细")
    @PreAuthorize("@ss.hasPermission('crossborder:order-shipment-item:update')")
    public CommonResult<Boolean> updateOrderShipmentItem(@Valid @RequestBody OrderShipmentItemSaveReqVO updateReqVO) {
        orderShipmentItemService.updateOrderShipmentItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跨境电商发货明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crossborder:order-shipment-item:delete')")
    public CommonResult<Boolean> deleteOrderShipmentItem(@RequestParam("id") Long id) {
        orderShipmentItemService.deleteOrderShipmentItem(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除跨境电商发货明细")
                @PreAuthorize("@ss.hasPermission('crossborder:order-shipment-item:delete')")
    public CommonResult<Boolean> deleteOrderShipmentItemList(@RequestParam("ids") List<Long> ids) {
        orderShipmentItemService.deleteOrderShipmentItemListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跨境电商发货明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:order-shipment-item:query')")
    public CommonResult<OrderShipmentItemRespVO> getOrderShipmentItem(@RequestParam("id") Long id) {
        OrderShipmentItemDO orderShipmentItem = orderShipmentItemService.getOrderShipmentItem(id);
        return success(BeanUtils.toBean(orderShipmentItem, OrderShipmentItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跨境电商发货明细分页")
    @PreAuthorize("@ss.hasPermission('crossborder:order-shipment-item:query')")
    public CommonResult<PageResult<OrderShipmentItemRespVO>> getOrderShipmentItemPage(@Valid OrderShipmentItemPageReqVO pageReqVO) {
        PageResult<OrderShipmentItemDO> pageResult = orderShipmentItemService.getOrderShipmentItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderShipmentItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跨境电商发货明细 Excel")
    @PreAuthorize("@ss.hasPermission('crossborder:order-shipment-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderShipmentItemExcel(@Valid OrderShipmentItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderShipmentItemDO> list = orderShipmentItemService.getOrderShipmentItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "跨境电商发货明细.xls", "数据", OrderShipmentItemRespVO.class,
                        BeanUtils.toBean(list, OrderShipmentItemRespVO.class));
    }

}